package com.jmyd.sharding.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.jmyd.sharding.base.dto.KeywordQueryDto;
import com.jmyd.sharding.base.service.impl.BaseServiceImpl;
import com.jmyd.sharding.base.vo.PageBean;
import com.jmyd.sharding.domain.DomainPoFactory;
import com.jmyd.sharding.domain.dto.SysCouponDto;
import com.jmyd.sharding.domain.dto.SysCouponUpdateDto;
import com.jmyd.sharding.domain.po.SysCoupon;
import com.jmyd.sharding.domain.vo.SysCouponVo;
import com.jmyd.sharding.mapper.SysCouponMapper;
import com.jmyd.sharding.service.ISysCouponService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author samphin
 * @date 2024/6/18 10:46
 */
@Service
public class SysCouponService extends BaseServiceImpl<Long, SysCouponDto, SysCoupon> implements ISysCouponService {

    @Resource
    private SysCouponMapper sysCouponMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SysCouponDto dto) {
        SysCoupon sysCoupon = DomainPoFactory.newInstance(SysCoupon.class, null).build(dto);
        sysCoupon.setDeleted(false);
        this.sysCouponMapper.insertSelective(sysCoupon);
        return true;
    }

    @Override
    public boolean updateById(SysCouponUpdateDto dto) {
        SysCoupon sysCoupon = new SysCoupon().build(dto);
        this.sysCouponMapper.updateByPrimaryKeySelective(sysCoupon);
        return true;
    }

    @Override
    public PageBean<SysCouponVo> paging(KeywordQueryDto queryDto) {
        final Page<Object> page = PageMethod.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        final List<SysCouponVo> voList = this.sysCouponMapper.paging(queryDto);
        return new PageBean<>(voList, page);
    }

    @Override
    public SysCouponVo queryDetail(Long id) {
        final SysCoupon sysCoupon = this.sysCouponMapper.selectById(id);
        return new SysCouponVo().buildVo(sysCoupon);
    }

    @Override
    public boolean deleteById(Long id) {
        this.sysCouponMapper.deleteById(id);
        return true;
    }
}